package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Maintainrecord;
import com.woniuxy.caradmin.entity.pojo.Oilrecord;
import com.woniuxy.caradmin.entity.vo.MaintainrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.MaintainrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
public class MaintainrecordController {
    @Autowired
    private MaintainrecordService maintainrecordService;

    @GetMapping("/maintainrecordList")
    @ApiOperation("查询所有的保养记录")
    public DataGridView selectAllMaintainRecord(MaintainrecordVo maintainrecordVo) {
        Page<Object> page = PageHelper.startPage(maintainrecordVo.getPage(), maintainrecordVo.getLimit());
        List<Maintainrecord> maintainrecords = maintainrecordService.selectAllMaintainRecord(maintainrecordVo);
        if(maintainrecords!=null && maintainrecords.size()>0){
            return  new DataGridView(page.getTotal(),maintainrecords);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISNULL, Constaint.MAINTAINRECORDISNULL);
        }
    }

    @GetMapping("/addMaintainRecord")
    @ApiOperation("增加保养记录")
    public R addMaintainRecord(Maintainrecord maintainrecord) {
        int addMaintainRecord = maintainrecordService.addMaintainRecord(maintainrecord);
        if (addMaintainRecord != 0) {
            return R.ok().code(ResultCode.MAINTAINRECORDISSUC).message(Constaint.MAINTAINRECORDISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISFAI, Constaint.MAINTAINRECORDISFAI);
        }
    }

    @GetMapping("/updateMaintainRecord")
    @ApiOperation("修改保养记录")
    public R updateMaintainRecord(Maintainrecord maintainrecord) {
        int updateMaintainRecord = maintainrecordService.updateMaintainRecord(maintainrecord);
        if (updateMaintainRecord != 0) {
            return R.ok().code(ResultCode.MAINTAINRECORDUPDATEISSUC).message(Constaint.MAINTAINRECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDUPDATEISFAI, Constaint.MAINTAINRECORDUPDATEISFAI);
        }
    }

    @GetMapping("/deleteMaintainRecord")
    public R deleteMaintainRecord(Integer id) {
        int deleteMaintainRecord = maintainrecordService.deleteMaintainRecord(id);
        if (deleteMaintainRecord != 0) {
            return R.ok().code(ResultCode.MAINTAINRECORDDELETEISSUC).message(Constaint.MAINTAINRECORDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDDDELETEISFAI, Constaint.MAINTAINRECORDDDELETEISFAI);
        }
    }

    @GetMapping("/goMaintainRecord")
    public ModelAndView goLOgin(ModelAndView modelAndView) {
        modelAndView.setViewName("maintainrecord");
        return modelAndView;
    }

}

